<?php
class Login_manage {

	var $CI;
	public function __construct() {

		$this->CI =& get_instance();
	}

	/**
	 *
	 *
		* @name login
		* @date 2013-1-9
		* @author eifel
		* @param 用户名,密码
		* @return 登录结果和登录信息
		* @version 1.0
		* @description
	 */
	public function login($user_name,$password,$captcha,$remember) {
		$this->CI->load->model("common/adm_user_model");
		$user_login_info	=$this->CI->adm_user_model->check_login($user_name);
		if ($user_login_info==null) {
			$data['result']	=false;
			$data['message']	="您输入的用户名不存在";
			return $data;
		}
		if ($user_login_info['status']=="0") {
			$data['result']	=false;
			$data['message']	="您的用户已经停用";
			return $data;
		}
		if ($this->need_captcha($user_name)) {
			if ($captcha=="") {
				$data['result']	=false;
				$data['message']	="您的帐号已经连续输错了5次密码,请返回主页输入验证码登录";
				return $data;
			}
			$this->CI->load->library("tools/captcha");
			if (!$this->CI->captcha->check_word($captcha)) {
				$data['result']	=false;
				$data['message']	="验证码输入有误";
				return $data;
			}
		}

		if ($user_login_info['password']!=md5($password)) {
			$data['result']	=false;
			$data['message']	="密码输入有误";
			$this->add_error_times($user_login_info['user_id'],$user_login_info['error_times']+1);
			return $data;
		}
		$this->add_error_times($user_login_info['user_id'],0);
		$this->user_login_session($user_login_info['user_id'], $user_name);
		$this->set_remember_user($user_name,$remember);
		$data['result']		=true;
		$data['message']	=$this->get_login_redirect_url();
		return $data;

	}


	public function mobile_login($user_name,$password){

		$this->CI->load->model("common/adm_user_model");
		
		$result['ret_code']=0;
		$result['error_code']=0;
		$result['msg']="";
		
		
		$user_login_info	=$this->CI->adm_user_model->check_login($user_name);
		if ($user_login_info==null) {
			$result['ret_code']=1;
			$result['error_code']=120;
			$result['msg']	="您输入的用户名不存在";
			return $result;
		}

		if ($user_login_info['password']!=md5($password)) {
			$result['ret_code']=1;
			$result['error_code']=121;
			$result['msg']	="您输入的密码有误";
			return $result;
		}
		$result['data']=null;
		$result['data']['token']=md5(uniqid(rand(), true));
		$this->CI->adm_user_model->update_user_token($user_login_info['user_id'],$result['data']['token']);
		return $result;
	}
	
	public function mobile_query_user_id($token){
		$this->CI->load->model("common/adm_user_model");
		return $this->CI->adm_user_model->query_user_id_by_token($token);
	}
	
	public function mobile_logout($user_id){
		$this->CI->load->model("common/adm_user_model");
		return $this->CI->adm_user_model->mobile_logout($user_id);
	}
	/**
	 *
	 *
		* @name user_login_session
		* @date 2013-1-9
		* @author eifel
		* @param 用户编号,用户名
		* @return
		* @version 1.0
		* @description 用户登录成功后设置session
	 */
	private  function user_login_session($user_id,$user_name) {
		$_SESSION['user_id']= $user_id;
		$_SESSION['user_name']= $user_name;
	}

	/**
	 *
	 *
		* @name need_captcha
		* @date 2013-1-9
		* @author eifel
		* @param
		* @return
		* @version 1.0
		* @description 判断是否需要登录验证码
	 */
	public  function need_captcha($user_name) {
		$this->CI->load->model("common/adm_user_model");
		$error_times_info	=$this->CI->adm_user_model->get_error_times($user_name);
		if ($error_times_info==null) {
			$error_times 	= 0;
		}else{
			$error_times	= $error_times_info['error_times'];
		}
		if ($error_times>=$this->CI->config->item("login_error_times")) {
			return true;
		}else{
			return false;
		}
	}

	/**
	 *
	 *
		* @name logout
		* @date 2013-1-9
		* @author eifel
		* @param
		* @return
		* @version 1.0
		* @description 注销登录
	 */

	public function logout() {
		session_destroy();
	}
	/**
	 *
	 *
		* @name set_remember_user
		* @date 2013-1-9
		* @author eifel
		* @param 用户名,是否记住用户名
		* @return
		* @version 1.0
		* @description 记住用户名
	 */
	private  function set_remember_user($user_name,$remember) {
		if ($remember==1) {
			setcookie("user_name",$user_name,time()+$this->CI->config->item("cookie_time"),"/");
		}else{
			setcookie("user_name",$user_name,time()-1,"/");
		}
	}

	/**
	 *
	 *
		* @name add_error_times
		* @date 2013-1-9
		* @author eifel
		* @param 用户ID,连续登录失败错误次数
		* @return
		* @version 1.0
		* @description 修改用户连续登录失败次数
	 */
	private  function add_error_times($user_id,$error_times) {
		$this->CI->adm_user_model->add_error_times($user_id,$error_times);
	}

	/**
	 *
	 *
		* @name check_login
		* @date 2013-1-9
		* @author eifel
		* @param
		* @return
		* @version 1.0
		* @description 检验是否登录
	 */
	public function check_login() {
		if (isset($_SESSION['user_name'])&&$_SESSION['user_name']!="") {
			return true;
		}else {
			return false;
		}
	}
	/**
	 *
	 *
		* @name redirect_url_check_login
		* @date 2013-1-9
		* @author eifel
		* @param
		* @return
		* @version 1.0
		* @description 进入系统时记录系统路径
	 */
	public function redirect_url_check_login() {

		if (!isset($_SESSION['user_name'])) {
			$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$_SESSION['redirect_url'] =$url;
			redirect(base_url());
		}
	}



	/**
	 *
	 *
		* @name get_login_redirect_url
		* @date 2013-1-9
		* @author eifel
		* @param
		* @return
		* @version 1.0
		* @description 登录成功后获取跳转地址
	 */
	private function get_login_redirect_url() {
		if (isset($_SESSION['redirect_url'])&&$_SESSION['redirect_url']!="") {
			return $_SESSION['redirect_url'];
		}else{
			return base_url()."index.php";
		}
	}
}